﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MyPoints.Model;
using MyPoints.UserControls;

namespace MyPoints
{
  /// <summary>
  /// Interaction logic for EditImageWindow.xaml
  /// </summary>
  public partial class EditImageWindow : Window
  {
    private AccordionExpanderExtension aee;

    private class VM : NotifyPropertyChanged
    {
      [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
      private string _Value = "";
      ///<summary>
      /// Sets/gets Value value. Default value is "".
      ///</summary>
      public string Value
      {
        get
        {
          return (_Value);
        }
        set
        {
          _Value = value;
          OnPropertyChanged(nameof(Value));
        }
      }

      [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
      private wptImageTypeType _Type =  wptImageTypeType.File;
      ///<summary>
      /// Sets/gets Type value. Default value is  wptImageTypeType.File.
      ///</summary>
      public wptImageTypeType Type
      {
        get
        {
          return (_Type);
        }
        set
        {
          _Type = value;
          OnPropertyChanged(nameof(Type));
        }
      }
    }
    private VM vm = new VM();

    public int UrlFileWidth { get
      {
        string s = (cmbImageSize.SelectedItem as ComboBoxItem).Content as string;
        int ret = int.Parse(s);
        return ret;
      }
    }

    public void Set(wptImageType value)
    {
      if (value != null)
      {
        vm.Value = value.Value;
        vm.Type = value.type;
      }

      switch (vm.Type)
      {
        case wptImageTypeType.Embedded:
          aee.Expand(expEmbedded);
          break;
        case wptImageTypeType.File:
          aee.Expand(expFile);
          break;
        case wptImageTypeType.Folder:
          aee.Expand(expFolder);
          break;
        case wptImageTypeType.Url:
          aee.Expand(expWeb);
          break;
        default:
          throw new NotSupportedException();
      }
    }
    public wptImageType Get()
    {
      if (expEmbedded.IsExpanded)
        vm.Type = wptImageTypeType.Embedded;
      else if (expFile.IsExpanded)
        vm.Type = wptImageTypeType.File;
      else if (expFolder.IsExpanded)
        vm.Type = wptImageTypeType.Folder;
      else if (expWeb.IsExpanded)
        vm.Type = wptImageTypeType.Url;

      wptImageType ret = new wptImageType()
      {
        Value = vm.Value,
        type = vm.Type
      };
      return ret;
    }

    public EditImageWindow()
    {
      InitializeComponent();
      aee = new AccordionExpanderExtension(pnlAccordion, expFile);
      this.DataContext = vm;
    }

    private void btnOk_Click(object sender, RoutedEventArgs e)
    {
      this.DialogResult = true;
      this.Hide();
    }
  }
}
